package com.suwish.pc.ui.tab;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

import com.android.ddmlib.AndroidDebugBridge;
import com.android.ddmlib.IDevice;
import com.suwish.device.util.DeviceUtils;
import com.suwish.device.util.Platform;
import com.suwish.pc.ui.component.UIImagePanel;
import com.suwish.pc.ui.component.UIRemoteFileExplorer;
import com.suwish.pc.ui.component.tab.UITabPanel;
import com.suwish.pc.ui.dialog.DevicePropDialog;
import com.suwish.pc.ui.main.MainFrame;
import com.suwish.pc.ui.util.DeviceHelper;
import com.suwish.pc.ui.util.UIHelper;

/**
 * 
 * 
 * @author Min
 *
 */
public class DeviceTabPanel extends UITabPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private IDevice device = null;
	private AndroidDebugBridge bridge = null;
	
	private JPanel devicePanel = null;
	private JPanel detailPanel = null;
	
	private UIImagePanel imagePanel = null;
	private JPanel imageToolBar = null;
	
	private UIRemoteFileExplorer remoteExplorer = null;
	
	public DeviceTabPanel(AndroidDebugBridge bridge, IDevice device){
		super();
		this.bridge = bridge;
		this.device = device;
		initUI();
		initData();
		initEvent();
	}
	
	private void initEvent(){}
	private void initData(){
		SwingUtilities.invokeLater(new Runnable() {			
			@Override
			public void run() {
				try{
					BufferedImage image = DeviceUtils.parseScreenshot(device, false);
					imagePanel.refresh(image);
				}catch(Exception ex){
					ex.toString();
				}
				
			}
		});
	}
	private void initUI(){
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		splitPane.setDividerLocation(360);
		splitPane.setDividerSize(0);
		splitPane.setOneTouchExpandable(false);
		splitPane.setResizeWeight(0);
		setLayout(new BorderLayout(0, 0));
		add(splitPane, BorderLayout.CENTER);
		devicePanel = new JPanel();
		devicePanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 5, 20));
		devicePanel.setBackground(Color.WHITE);
		devicePanel.setLayout(new BoxLayout(devicePanel, BoxLayout.PAGE_AXIS));
		
		imagePanel = new UIImagePanel();
//		pane.setPreferredSize(pane.getPreferredSize());
		imagePanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		imagePanel.setAlignmentY(Component.TOP_ALIGNMENT);
		devicePanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		devicePanel.setAlignmentY(Component.TOP_ALIGNMENT);
		devicePanel.add(imagePanel);
		
		splitPane.setLeftComponent(devicePanel);
		
		imageToolBar = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 0));
		imageToolBar.setOpaque(false);
		imageToolBar.setAlignmentX(Component.CENTER_ALIGNMENT);
		imageToolBar.setAlignmentY(Component.TOP_ALIGNMENT);
		devicePanel.add(imageToolBar);
		initImageToolBar();
		detailPanel = new JPanel();
		detailPanel.setBackground(Color.WHITE);
		initDetailPanel();
		JScrollPane scrollPane = new JScrollPane(detailPanel);
		scrollPane.setBorder(BorderFactory.createEmptyBorder());
		splitPane.setRightComponent(scrollPane);
	}
	
	
	private void initDetailPanel(){
		detailPanel.setLayout(new BorderLayout());
		String title = "<html><font color='rgb(34, 153, 57)' size='25'>&nbsp;" + getTitle() + "</font><br/>" + 
				"<font color='gray'>&nbsp;&nbsp;&nbsp;" + device.getProperty(Platform.PROP_OR_BUILD_PRODUCT) + "&nbsp;&nbsp;&nbsp;" +
				"Android&nbsp;&nbsp;" + device.getProperty(Platform.PROP_OR_BUILD_VERSION_RELEASE)
				+ "</font></html>";
		JLabel titleLabel = new JLabel(title);
//		titleLabel.setForeground(new Color(34, 153, 57));
//		titleLabel.setFont(new Font(Font.DIALOG, Font.BOLD, 25));
		detailPanel.add(titleLabel, BorderLayout.NORTH);
		//本来放手机信息，已安装的软件什么，但是纯PC端实现不了，于是先放个文件浏览器
		remoteExplorer = new UIRemoteFileExplorer(device);
		detailPanel.add(remoteExplorer, BorderLayout.CENTER);
		JPanel buttomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 5));
		buttomPanel.setOpaque(false);
		detailPanel.add(buttomPanel, BorderLayout.SOUTH);
		JButton installButton = new JButton("Install APK");
		installButton.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {				
				try{
					DeviceHelper.installAPK(device);
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
		});
		buttomPanel.add(installButton);
		JButton uninstallButton = new JButton("UnInstall APK");
		uninstallButton.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					UIHelper.uninstallApk(device);
				}catch(Exception ex){
					UIHelper.showErrorDialog(ex);
				}
				
			}
		});
		buttomPanel.add(uninstallButton);
	}
	private void initImageToolBar(){
		JButton screenCutButton = new JButton("Screen shot");
		screenCutButton.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				UIHelper.saveScreenshot(device);
			}
		});
		imageToolBar.add(screenCutButton);
		JButton reFreshButton = new JButton("Refresh");
		reFreshButton.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				try{
					BufferedImage image = DeviceUtils.parseScreenshot(device, false);
					imagePanel.refresh(image);
				}catch(Exception ex){
					ex.toString();
				}
			}
		});
		imageToolBar.add(reFreshButton);
		JButton getPropButton = new JButton("Get Prop");
		getPropButton.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				new DevicePropDialog( MainFrame.getInstance(), device).setVisible(true);
				
			}
		});
		imageToolBar.add(getPropButton);
	}
	@Override
	public String getName(){
		return device.getSerialNumber();
	}
	@Override
	public TabType getTabType() {
		return TabType.DEVICE;
	}

	@Override
	public boolean close() {
		return true;

	}
	@Override
	public String getTip() {
		return device.getSerialNumber() + "(" + getTitle() + ")";
	}
	@Override
	public String getTitle() {
		return device.getProperty(Platform.PROP_OR_PRODUCT_MANUFACTURE) + 
			" " + device.getProperty(Platform.PROP_OR_PRODUCT_MODEL);
	}
	@Override
	public Icon getIcon() {
		// TODO Auto-generated method stub
		return null;
	}

}
